var platform = {
    tbdf: '淘宝垫付',
    tbll: '淘宝浏览'
};

var maintype = {
    tb: 1,
    jd: 2
};

var detailType = ['pay', 'browse'];

var param = null;

$(init);


function init() {

    $('body').on('click', 'ons-card#ll-tb-btn', pushTbLlOrders)
             .on('click', 'ons-card#df-tb-btn', pushTbDfOrders)
             .on('click', 'ons-card.df-order', getDFOrderDetail)
             .on('click', 'ons-card.ll-order', getLLOrderDetail)
             .on('click', 'ons-button.take-order-btn', takeOrder)
             .on('click', 'ons-fab#attention-fab', loadAttention)
             .on('click', 'ons-fab#df-example', showDfExample);


    document.addEventListener('init', function (event) {
        var page = event.target;
        if (page.matches('#tb-ll-orders')) {
            loadTbLlOrders();
        } else if (page.matches('#tb-df-orders')) {
            loadTbDfOrders();
        }
    });
}

function showDfExample() {
    navPushPage('/mobile-views/_orders/_df/df-example.html');
}

function takeOrder() {
    var msg = '';
    if( $(this).attr('id') === 'df') {
        msg = TEXT_PAY_TASK_INFO;
    }else if ($(this).attr('id') === 'll') {
        msg = TEXT_BROWSER_TASK_INFO;
    }

    ons.notification.confirm({
        message: msg,
        callback: function (idx) {
            switch (idx) {
                case 0:
                    break;
                case 1:
                    var obj = { taskkeyId: $('div.taskkeyid').attr('id') };
                    promise('GET',[URL_BUYER_GET_TASK, encodeQuery(obj)].join('?'), null, cbChoose, null);

                    break;
            }
        }
    });

}

function cbChoose() {
    notifyToast('接单成功！');
    document.querySelector('ons-navigator').removePage(1);
    document.querySelector('ons-navigator').removePage(1);
    document.querySelector('#main-tabbar').setActiveTab(2);
}


function getOrderDetail(id, type) {
    navPushPage('/mobile-views/_detail/each-order-detail.html');
    var obj = {
        taskkeyid: id,
        type: type
    }
    promiseTmpl('GET', '/mobile-tmpl/each-order-render.tmpl', [URL_BUY_TASKDETAIL, encodeQuery(obj)].join('?'), null, cbGetDetial);
}

function getLLOrderDetail() {
    var id = $(this).attr('id');
    var type = detailType[1];
    getOrderDetail(id, type);
}

function loadAttention() {
    navPushPage('/mobile-views/_orders/attention.html');
}

function getDFOrderDetail() {
    var id = $(this).attr('id');
    var type = detailType[0];
    getOrderDetail(id, type);
}

function cbGetDetial(r, e) {
    var ret = e;
    console.log(e);
    // console.log(e);
    $('ons-page#each-order-detail div.page__content').html('');
    $('ons-page#each-order-detail div.page__content').append($.templates(r).render(ret, rdHelper));
}

function setLoader() {
}

function loadTbLlOrders() {
    param = {
        mainType: maintype.tb,
        platform: platform.tbll,
        pageIndex: 0,
        pageSize: 666
    };
    promiseTmpl('GET', '/mobile-tmpl/tb-ll-render.tmpl', [URL_BUYER_ALL_TASK, encodeQuery(param)].join('?'),null, cbOrderList);
}

function loadTbDfOrders(r) {
    param = {
        mainType: maintype.tb,
        platform: platform.tbdf,
        pageIndex: 0,
        pageSize: 666
    };
    promiseTmpl('GET', '/mobile-tmpl/tb-df-render.tmpl', [URL_BUYER_ALL_TASK, encodeQuery(param)].join('?'),null, cbOrderList);
}

function cbOrderList(r, e) {
    var ret = e;
    if(cookie('approveState')!==1 || cookie('taobaoList')[0].approve !== 1) {
        notifyInfo('请先完成实名认证和淘宝绑定！');
        return;
    }
    if(ret.data.length === 0) return;
    if( param.platform === platform.tbdf) {
        $('ons-page#tb-df-orders div.page__content').html('');
        $('ons-page#tb-df-orders div.page__content').append($.templates(r).render(ret, rdHelper));
    }
    if (param.platform === platform.tbll) {
        $('ons-page#tb-ll-orders div.page__content').html('');
        $('ons-page#tb-ll-orders div.page__content').append($.templates(r).render(ret, rdHelper));
    }
}

function pushTbLlOrders() {
    navPushPage('/mobile-views/_orders/_ll/tb-ll-orders.html');
}

function pushTbDfOrders() {
    navPushPage('/mobile-views/_orders/_df/tb-df-orders.html');
}